package com.example.jiangqi.web.controller;

import com.example.jiangqi.dao.ClassroomMapper;
import com.example.jiangqi.entity.Classroom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RequestMapping("classroom")
@Controller
public class ClassroomController {
    @Autowired
    ClassroomMapper classroomMapper;

    @RequestMapping("list")
    @ResponseBody
    public List<Classroom> list(){
        List<Classroom>list=classroomMapper.list();
        return list;
    }

    @RequestMapping("listhtml")
    public String listhtml(ModelMap map){
        List<Classroom> list=classroomMapper.list();
        map.put("classroomList",list);
        return "/classroom/list";
    }

    @RequestMapping("add")
    public String add(){
        return "/classroom/add";
    }

    @PostMapping("save")
    public String save(Classroom cls){
        classroomMapper.save(cls);
        return "redirect:/classroom/listhtml";
    }

    @PostMapping("update")
    public String updateSave(Classroom cls){
        classroomMapper.update(cls);
        return "redirect:/classroom/listhtml";
    }
    @GetMapping("update/{clsId}")
    public String update(@PathVariable("clsId") String clsId, ModelMap map){

        Classroom cls = classroomMapper.seleteUserById(Long.parseLong(clsId));
        map.put("classroom",cls);
        return "/classroom/update";
    }
    @RequestMapping("delete/{clsId}")
    public String delete(@PathVariable("clsId") long clsId){
        classroomMapper.delete(clsId);
        return "redirect:/classroom/listhtml";
    }
}
